<?php 
namespace plugins\appmarket\model;
use think\Model;
use think\Db;

/**
 * 
 */
class AdminIndexModel extends Model
{
	protected $pageNum  = 20 ;
	/**
	 * 获取模块市场数据
	 * $userId 用户id
	 */
	public function appList( $userId , $field = "" , $search = [] )
	{
		$purchaseModuleIds = $this->getPurchaseHistory( $userId );
		$company_id = Db::name( 'user_attach' )->where( 'user_id' , $userId )->value( 'company_id' );
		
		$moduleList = Db::name( 'module_store' )->where( 'putway' , 1 );
		/*if( $company_id != 8 ){
			$menus = Db::name( 'module_menu' )->where( 'company_id' , $company_id )->column( 'menu_id' );
			$moduleList = $moduleList-> whereOr( 'menu_id','IN' ,$menus );
		}*/
		if( !empty($field) ){
			$field = is_array( $field ) ? implode(',' , $field ) : $field ; 
			$moduleList = $moduleList->field( $field );
		}
		Db::name('test')->insert(['con'=>json_encode($search)]);
		//搜索
		$query = [] ;
		if( !empty ( $search ['search']  ) ){
			$query = [ 'query' => $search ];
			//是否免费
			if( $search['payment'] == 0 ){
				$moduleList = $moduleList->where( 'price' , '=' , 0  );
			}else if( $search ['payment'] == 1 ){
				$moduleList = $moduleList->where( 'price' , '>' , 0  );
			}
			//分类
			if( $search ['category'] != 0 ){
				$moduleList = $moduleList->where( 'category' , '=' , $search ['category']  );
			}
			
			//我购买的
			if( $search [ 'buy' ] != 0 ){
				$moduleList = $moduleList->where( 'id' , 'IN' , $purchaseModuleIds  );
			}
			
			//关键字
			if( $search [ 'keywords' ] != '' ){
				$moduleList = $moduleList->where( "concat(`title`,`describe`) REGEXP '".$search['keywords']."'" );
			}
		}

		//$moduleList = $moduleList->paginatePlus(20);
		
		$moduleList = $moduleList->order('name,status desc')->paginate( $this->pageNum , false , $query );
		
		$page = $moduleList->render();
		foreach ($moduleList as $key => $value) {
			$name = cmf_parse_name( $value ['name'] , 1 );
			//标记已购买的
			$isBuy = 0;
			if( in_array( $value['id'] , $purchaseModuleIds ) ){
				$isBuy = 1;
			}
			//标记是否安装
			if( $company_id == 8 ){
				$isInstall = Db::name( 'plugin' )->where( [ 'name' => $name ] )->find();
				$value [ 'installed' ] =  empty( $isInstall) ? 0 : 1 ;
			}else{
				//注释段代码已废弃2020317
				$menu_id = Db::name( 'admin_menu' )->where( ['app' => "plugin/$name" , 'controller' => 'AdminIndex' , 'action' => 'index' ] ) -> value( 'id' );
				
				$module_id = Db::name('module_menu')->where( [ 'menu_id'=>$menu_id, 'company_id'=>$company_id ] )->value('module_id');
				if( empty( $module_id ) ){
					//老系统 1.0
					$value ['installed'] = $this->getInstalled( $menu_id , $company_id , $value['id'] );
				}else{
					$isInstall = Db::name('module_menu')->where( 'module_id' , $value[ 'id' ] )->find();
					$value[ 'installed' ] = empty( $isInstall ) ? 0 : 1 ;
				}
				
			}
			$value ['bought'] = $isBuy;
			//分类名称
			$value ['category'] = Db::name( 'module_category' )->where( 'id' , $value ['category'] )->value( 'name' );

			$moduleList [ $key ] = $value;
		}
		return $moduleList ;
	}



	/**
	 * 获取购买的模块数据
	 */
	public function getPurchaseModule( $userId )
	{
		$purchaseModuleIds = $this->getPurchaseModule( $userId );
		$list = Db::name( 'module_store' )->where( 'id' , $purchaseModuleIds )->select()->toArray();
		return $list ;
	}

	/**
	 * 根据用户id 获取购买的模块id
	 * @param  [type] $userId [description]
	 * @return [type]         [description]
	 */
	public function getPurchaseHistory( $userId)
	{
		$purchaseModuleIds = Db::name( 'module_purchase_history' )->where( 'user_id' , $userId )->where('status',1)->column('module_id');
		return $purchaseModuleIds;
	}
	
	/**
	*获取已安装的数据
	*
	**/
	public function getInstalled( $menu_id , $company_id , $module_id )
	{
		//
		$where = [
			'menu_id' =>$menu_id,
			'company_id' => $company_id 
		];
		
		$find = Db::name('module_menu')->where( $where )->value( 'id' );
		
		if( empty ($find) ){
			return 0;
		}
		//是否购买的是此版本的模块
		$module_purchase_history_id = Db::name( 'module_purchase_history' )->where( ['module_id'=> $module_id , 'company_id'=>$company_id ] )->value( 'id' );
		if( empty($module_purchase_history_id) ){
			return 0;
		}
		
		return 1 ;
	}

	/**
	 * 购买操作 记录购买数据
	 * dbtable: cmf_module_purchase_history
	 */

}